import Vue from 'vue'
import Router from 'vue-router'
import store from '@store'

// in development-env not use lazy-loading, because lazy-loading too many pages will cause webpack hot update too slow. so only in production use lazy-loading;
// detail: https://panjiachen.github.io/vue-element-admin-site/#/lazy-loading

Vue.use(Router)

/* Layout */
import Layout from '../views/layout/Layout'

/**
* hidden: true                   if `hidden:true` will not show in the sidebar(default is false)
* alwaysShow: true               if set true, will always show the root menu, whatever its child routes length
*                                if not set alwaysShow, only more than one route under the children
*                                it will becomes nested mode, otherwise not show the root menu
* redirect: noredirect           if `redirect:noredirect` will no redirct in the breadcrumb
* name:'router-name'             the name is used by <keep-alive> (must set!!!)
* meta : {
    title: 'title'               the name show in submenu and breadcrumb (recommend set)
    icon: 'svg-name'             the icon show in the sidebar,
  }
**/

const test = () => import('@/views/dashboard/index')
export const constantRouterMap = [
  { path: '/login', component: () => import('@/views/login/index'), hidden: true },
  { path: '/404', component: () => import('@/views/404'), hidden: true },

  {
    path: '/',
    component: Layout,
    redirect: '/home',
    name: '首页',
    hidden: true,
    children: [{
      path: 'home',
      component: () => import('@/views/dashboard/index'),
      meta: { title: '首页' }
    }]
  }
]

const router = new Router({
  mode: 'history', //后端支持可开
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRouterMap
})

export default router;

// 返回路由组件
function getMatchedComponent(path) {
  const matchedComponent = router.getMatchedComponents(path);
  if (!matchedComponent.length) return null;
  return matchedComponent[matchedComponent.length - 1];
}

router.beforeEach((to, from, next) => {
  // 获取当前路由匹配的组件
  const matchedComponent = getMatchedComponent(to);
  store.dispatch('ChangeRouteComponent', matchedComponent);
  next();
});

export const asyncRouterMap = [{
    path: '/product',
    component: Layout,
    children: [{
      path: 'index',
      name: 'product',
      component: () => import('@/views/product/index'),
      meta: { title: '产品列表', icon: 'product-hunt', roles: ['admin', 'sys'] },
      children: [{
        path: 'detail/:id',
        name: 'productDetail',
        component: () => import('@/views/product/detail'),
        hidden: true,
        meta: { title: '产品详情', icon: 'product-hunt', roles: ['admin', 'sys'] }
      }]
    }]
  },
  {
    path: '/device',
    component: Layout,
    redirect: '/device/index',
    name: 'device',
    meta: { title: '设备管理', icon: 'server', roles: ['admin'] },
    children: [{
        path: 'index',
        name: 'deviceList',
        component: () => import('@/views/device/index'),
        meta: { title: '设备列表', icon: 'list', roles: ['admin', 'sys'] },
        children: [{
          path: 'detail/:id',
          name: 'deviceDetail',
          hidden: true,
          component: () => import('@/views/device/detail'),
          meta: { title: '设备详情', icon: 'list', roles: ['admin', 'sys'] }
        }]
      },
      {
        path: 'check',
        name: 'deviceCheck',
        component: () => import('@/views/device/check'),
        meta: { title: '设备审核', icon: 'dedent', roles: ['admin', 'sys'] }
      }
    ]
  },
  {
    path: '/system',
    component: Layout,
    redirect: '/system/user',
    name: 'system',
    meta: { title: '系统管理', icon: 'th-large', roles: ['admin'] },
    children: [{
        path: 'user',
        name: 'user',
        component: () => import('@/views/system/user'),
        meta: { title: '用户管理', icon: 'user', roles: ['admin'] }
      },
      {
        path: 'role',
        name: 'role',
        component: () => import('@/views/system/role'),
        meta: { title: '角色管理', icon: 'users', roles: ['admin'] }
      },
      {
        path: 'setting',
        name: 'setting',
        component: () => import('@/views/system/setting'),
        meta: { title: '系统配置', icon: 'cog', roles: ['admin'] }
      }
    ]
  },
  { path: '*', redirect: '/404', hidden: true }
]
